require "lfs"
require "unity"
local CreateRadome = require "luaCreateRadome"
local nl = require"luaNumeric"


--===========================================================================--
--初始化参数
local RadomeDesigner={
	console_mode = false;
	inner_radius = 3;
	uniform_height = 5;
	ant_radius = 100;
	ant_posx= 500;
	ant_posy = 0;
	freq = 16;
	epsr = 3;
	order = 1;
};
--===========================================================================--

--===========================================================================--
--创建表和创建元表
function RadomeDesigner:new (o)
	o = o or {};
	self.__index = self;
	setmetatable(o,self)
	return o;
end
--===========================================================================--

--===========================================================================--
--导入模型
function RadomeDesigner:load ( path )
	--self.update_parameters_to_ui()
	self:update_parameters_from_ui()
	Geometry.clear_all();
	self.path = path;
	self.radome = Geometry.load_step(path);
	 --Geometry.AddShape(self.radome)
	--Geometry.View:perspective_view()
	local CreateRadome = require "luaCreateRadome"
	self.analyzer = CreateRadome.RadomeGenerator:new(401,0,100,5,10);

	assert(self.analyzer:import_from_file(path)>=0, "import file error!\n"..path );
	
	--self:perspective_view()
	--assert(self.analyzer:isSolid(), "导入模型不包含实体!" );
	--assert(self.analyzer:numSolid()==1, "导入模型超过一个实体!" );

	assert(self.analyzer:checkOrientation(), " 天线罩大端未朝向x轴正向" );
	Gallop.View:perspective_view()
		
	Geometry.AddShape(self.analyzer:glShapeRadomeOuter());
end
--===========================================================================--

--===========================================================================--
--生成天线罩
function RadomeDesigner:generate_uniform_radome ( )
	self:update_parameters_from_ui()
	print( self.uniform_height )
	local fun = function(d)
		return self.uniform_height;
	end;
	self.analyzer:create_inner_from_height_function(fun);
	self.analyzer:fillet_inner_edge();
	local shape = self.analyzer:inner_edge();
	shape:name('inner edge of uniform height:'..self.uniform_height)
	--Geometry.AddShape(shape);
	local shape = self.analyzer:inner_wire();
	shape:name('inner wire of uniform height:'..self.uniform_height)
	--Geometry.AddShape(shape);
	
	--create_inner_from_tmm_opt_height
	self:generate_radome()
	
	local shape = self.analyzer:radome();
	shape:name('Radome of height:'..self.uniform_height)
	shape:heal(0.1,true,true,true,true,true)
	Geometry.AddShape(shape);
end
--===========================================================================--

--===========================================================================--
--生成天线罩
function RadomeDesigner:generate_nonuniform_radome()
	self:update_parameters_from_ui()
	print(self.ant_posx,self.ant_posy,self.epsr,self.freq,self.order)
	self.analyzer:create_inner_from_tmm_opt_height(self.ant_posx,self.ant_posy,self.epsr,self.freq*1e9,self.order);
	self.analyzer:fillet_inner_edge();
	local shape = self.analyzer:inner_edge();
	shape:name('inner edge of order:'..self.order .. '@ freq:' .. self.freq)
	Geometry.AddShape(shape);
	local shape = self.analyzer:inner_wire();
	shape:name('inner wire of order:'..self.order .. '@ freq:' .. self.freq)
	Geometry.AddShape(shape);
	
	self:generate_radome()
	
	local shape = self.analyzer:inner_rotator();
	shape:name('inner rotator of order:'..self.order .. '@ freq:' .. self.freq)
	--Geometry.AddShape(shape);
	
	local shape = self.analyzer:outer_rotator();
	shape:name('outer rotator of order:'..self.order .. '@ freq:' .. self.freq)
	--Geometry.AddShape(shape);
	
	local shape = self.analyzer:radome();
	shape:name('Radome of order:'..self.order .. '@ freq:' .. self.freq)
	shape:heal(0.1,true,true,true,true,true)
	Geometry.AddShape(shape);

end
--===========================================================================--

--===========================================================================--
--生成天线罩实体
function RadomeDesigner:generate_radome()
	--self.analyzer:rotate_inner()
	--self.analyzer:rotate_outer()
	self.analyzer:rotate()
end
--===========================================================================--

return RadomeDesigner;
